package com.hcj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcj.mapper.ProductMapper;
import com.hcj.mapper.CategoryMapper;
import com.hcj.pojo.Category;
import com.hcj.pojo.Product;
import com.hcj.service.ProductService;
import com.hcj.util.MyResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper mapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public MyResponse add(Product product) {
        // 1. 看这个商品名有没有重复
        Product product1 = mapper.selectOne(new LambdaQueryWrapper<Product>()
                .eq(Product::getName, product.getName()));

        if (product1 != null) {
            return new MyResponse("200", "商品已存在！");
        }

        int insert = mapper.insert(product);

        if (insert == 1) {
            return new MyResponse("100", "商品添加成功!");
        }

        return new MyResponse("200", "商品信息插入异常!");
    }



    @Override
    public MyResponse update(Product product) {

        // 根据 id 来对商品信息做更新

        // 判断 product 是否携带要修改的商品的id
        Integer id = product.getId();
        if (id == null) {
            return new MyResponse("200", "要修改的商品不存在！");
        }

        // 判断 这个 id 的商品是否真的存在
        Product product1 = mapper.selectOne(new LambdaQueryWrapper<Product>()
                .eq(Product::getId, product.getId()));

        if (product1 == null) {
            return new MyResponse("100", "要修改的商品不存在！");
        }

        // 最后执行更新
        int i = mapper.updateById(product);

        if (i == 1) {
            return new MyResponse("100", "商品信息修改成功!");
        }
        return new MyResponse("100", "商品信息修改异常!");
    }
    @Override
    public boolean delete(Integer id) {

        LambdaQueryWrapper<Product> eq = new LambdaQueryWrapper<Product>().eq(Product::getId, id);
        int delete = mapper.delete(eq);
        return delete==1;
    }


    @Override
    public Page<Product> selectByNameOrDetailPaging(String keyword, Integer currentPage, Integer pageSize) {
        // 创建一个分页对象，并设置分页参数
        Page<Product> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);

        // 创建一个条件对象，设置查询条件
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .like(Product::getName, "%" + keyword + "%")
                .or()
                .like(Product::getDetail, "%" + keyword + "%");


        Page<Product> productPage = mapper.selectPage(page, wrapper);
        productPage.getRecords();
        return productPage;
    }

    @Override
    public Page<Product> selectAllPaging(Integer currentPage, Integer pageSize) {
        // 创建一个分页对象，并设置分页参数
        Page<Product> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);
        ;

        return mapper.selectPage(page, null);
    }

    @Override
    public Product selectOneById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public List<Product> selectByCategory(String category) {
        return mapper.selectByCategory(category);
    }

    @Override
    public Page<Product> selectByCategory(String category, Integer currentPage, Integer pageSize) {

        // 1. 查询 category 的 id 是多少
        Category category1 = categoryMapper.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getName, category));

        // 如果没有这个分类
        if (category1 == null) {
            return null;
        }



        // 2. 执行分页查询 product cid = id
        Integer id = category1.getId();

        Page<Product> productPage = new Page<>();
        productPage.setSize(pageSize);
        productPage.setCurrent(currentPage);

        Page<Product> productPage1 = mapper.selectPage(productPage,
                new LambdaQueryWrapper<Product>()
                        .eq(Product::getCid, id));

        return productPage1;
    }
    @Override
    public List<Product> selectByPid(Integer pid)
    {
        return mapper.selectList(new LambdaQueryWrapper<Product>().eq(Product::getId,pid));
    }

    @Override
    public  Page<Product> selectByStatus(Integer status,Integer currentPage,Integer pageSize){
        Page<Product> productPage = new Page<>();
        productPage.setSize(pageSize);
        productPage.setCurrent(currentPage);

        return mapper.selectPage(productPage,new LambdaQueryWrapper<Product>().eq(Product::getStatus,status));
    }
    @Override
    public Product selectByName(String name){
        return mapper.selectOne(new LambdaQueryWrapper<Product>().eq(Product::getName,name));
    }
    @Override
    public List<Product> selectByLikeName(String name){
        return mapper.selectList(new LambdaQueryWrapper<Product>().like(Product::getName,name));
    }


}
